using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DrinkTargets : MonoBehaviour
{
	private GlassEdge glassEdge;

	private Transform parentTransform;

	private Transform minLiquidLevel;

	private Transform maxLiquidLevel;

	private float targetLineWidth;

	private List<TargetLine> targetLines;

	private int currentTargetLineIndex;

	private float heightOfLiquidsInFullGlass;

	private float liquidsLevelInFullGlass;

	private float scoredLiquidLevel = 0.35f;

	[HideInInspector]
	public List<LiquidType> targetLiquidTypes;

	[HideInInspector]
	public List<float> targetLiquidHeights;

	private void Awake()
	{
		DefineConnections();
		targetLines = new List<TargetLine>();
		CalculateHeightOfLiquidsInFullGlass();
		targetLineWidth = glassEdge.GetGlassMaxWidth() + 0.2f;
		CalculateHighestLiquidLevel();
	}

	private void DefineConnections()
	{
		parentTransform = base.transform.FindDeepChild("TargetLines");
		glassEdge = GetComponent<GlassEdge>();
		minLiquidLevel = base.transform.FindDeepChild("MinLiquid");
		maxLiquidLevel = base.transform.FindDeepChild("MaxLiquid");
	}

	private void CalculateHeightOfLiquidsInFullGlass()
	{
		Vector2 vector = maxLiquidLevel.position;
		Vector2 vector2 = minLiquidLevel.position;
		heightOfLiquidsInFullGlass = vector.y - vector2.y;
	}

	public void CalculateHighestLiquidLevel()
	{
		liquidsLevelInFullGlass = 1.15f;
	}

	public void SetTargetsData(List<LiquidType> liquidTypes, List<float> liquidHeights)
	{
		targetLiquidTypes = liquidTypes;
		targetLiquidHeights = liquidHeights;
	}

	public void SetTargets()
	{
		for (int i = 0; i < targetLiquidTypes.Count; i++)
		{
			float previousLiquidLevel = 0f;
			float nextLiquidLevel = 200f;
			if (i > 0)
			{
				previousLiquidLevel = targetLiquidHeights[i - 1];
			}
			if (i + 1 < targetLiquidTypes.Count)
			{
				nextLiquidLevel = targetLiquidHeights[i + 1];
			}
			SetTarget(targetLiquidTypes[i], targetLiquidHeights[i], previousLiquidLevel, nextLiquidLevel);
		}
	}

	public void SetTarget(LiquidType liquidType, float liquidPercentage, float previousLiquidLevel, float nextLiquidLevel)
	{
		GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load("Prefabs/GlassEssentials/TargetLine"), parentTransform) as GameObject;
		Vector3 position = minLiquidLevel.position;
		float y = position.y + liquidPercentage / 100f / liquidsLevelInFullGlass * heightOfLiquidsInFullGlass;
		Vector3 position2 = default(Vector3);
		position2.y = y;
		Vector3 position3 = base.transform.position;
		position2.x = position3.x - targetLineWidth * 0.5f;
		position2.z = 7f;
		gameObject.transform.position = position2;
		float num = (liquidPercentage - previousLiquidLevel) * scoredLiquidLevel / 100f * heightOfLiquidsInFullGlass / liquidsLevelInFullGlass;
		float num2 = num;
		if ((liquidPercentage - previousLiquidLevel) * scoredLiquidLevel > (nextLiquidLevel - liquidPercentage) * 0.5f)
		{
			num2 = (nextLiquidLevel - liquidPercentage) * 0.5f / 100f * heightOfLiquidsInFullGlass / liquidsLevelInFullGlass;
		}
		gameObject.GetComponent<TargetLine>().SetTargetLine(liquidType, targetLineWidth, num, num2);
		float num3 = position2.y + num2;
		Vector3 position4 = maxLiquidLevel.position;
		if (num3 > position4.y)
		{
			Vector3 position5 = maxLiquidLevel.position;
			num2 = position5.y - position2.y;
			gameObject.GetComponent<TargetLine>().SetTargetLineAsReachingGlassEdge(num2);
		}
		targetLines.Add(gameObject.GetComponent<TargetLine>());
	}

	public void ShowTargetLines()
	{
		StartCoroutine(ShowingTargetLines());
		StartCoroutine(UnlockControlls());
	}

	private IEnumerator UnlockControlls()
	{
		BottleController.blockAllBottlesController = true;
		ButtonPickLiquidController.blockAllButtonPickLiquidController = true;
		yield return new WaitForSeconds(0.2f);
		if (UnityEngine.Object.FindObjectOfType<PausePanel>() == null)
		{
			BottleController.blockAllBottlesController = false;
		}
		yield return new WaitForSeconds(1f);
		ButtonPickLiquidController.blockAllButtonPickLiquidController = false;
	}

	private IEnumerator ShowingTargetLines()
	{
		foreach (TargetLine targetLine in targetLines)
		{
			targetLine.ShowTargetLine();
			yield return new WaitForSeconds(0.35f);
		}
	}

	public TargetLine GetCurrentTargetLine()
	{
		return targetLines[currentTargetLineIndex];
	}

	public void SetNextCurrentTargetLine(float currentLiquidLevel)
	{
		if (!GetCurrentTargetLine().IsLiquidLevelBelowScoredRange(currentLiquidLevel))
		{
			targetLines[currentTargetLineIndex].HideTargetLine();
		}
		currentTargetLineIndex++;
		if (currentTargetLineIndex >= targetLines.Count)
		{
			Object.FindObjectOfType<GameController>().DrinkReady();
		}
	}

	public void SetInitialCurrentTargetLine()
	{
		currentTargetLineIndex = 0;
		ShowTargetLines();
	}

	public List<TargetLine> GetTargetLinesList()
	{
		return targetLines;
	}

	public float GetMaxLiquidPosition()
	{
		Vector3 position = maxLiquidLevel.position;
		return position.y;
	}
}
